package com.real.operationalcontrols.登陆;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.king.base.baseurlmanager.BaseUrlManagerActivity;
import com.mob.tools.utils.DeviceHelper;
import com.real.operationalcontrols.App;
import com.real.operationalcontrols.MainActivity;
import com.real.operationalcontrols.R;
import com.real.operationalcontrols.rxhttp.MyDialogObserver;
import com.real.operationalcontrols.rxhttp.NetworkUrl;
import com.real.operationalcontrols.rxhttp.RxHttpManger;
import com.real.operationalcontrols.基类.BaseActivity;
import com.real.operationalcontrols.实体类.LoginBean;
import com.white.easysp.EasySP;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

@RuntimePermissions
public class LoginActivity extends BaseActivity {

    @BindView(R.id.bar_back_btn)
    ImageView barBackBtn;
    @BindView(R.id.bar_title)
    TextView barTitle;
    @BindView(R.id.bar_check_btn)
    TextView barCheckBtn;
    @BindView(R.id.ed_username)
    EditText edUsername;
    @BindView(R.id.ed_password)
    EditText edPassword;
    @BindView(R.id.check_web_btn)
    TextView checkWebBtn;
    @BindView(R.id.login_btn)
    TextView loginBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_login);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void getData() {
        barBackBtn.setVisibility(View.GONE);
        barTitle.setText("登 录");
        LoginActivityPermissionsDispatcher.callWithPermissionCheck(this);
    }


    private void login() {
        String user = edUsername.getText().toString().trim();
        String psw = edPassword.getText().toString().trim();
        if (user.isEmpty()) {
            edUsername.setError("请输入账号");
            edUsername.requestFocus();
            return;
        }
        if (psw.isEmpty()) {
            edPassword.setError("请输入密码");
            edPassword.requestFocus();
            return;
        }
//        String baseUrl = App.getBaseUrl();
//        if (baseUrl.isEmpty()) {
//            ToastUtils.showShortToast("请选择服务器地址");
//            return;
//        }

        String imei = DeviceHelper.getInstance(this).getAndroidID();
        Map<String, String> map = new HashMap<>();
        map.put("username", user);
        map.put("password", psw);
        map.put("uid", imei);
        RxHttpManger.getInstance().post(this, NetworkUrl.LOGIN, map, new MyDialogObserver(this) {
            @Override
            public void onSuccess(String response) {
                LoginBean bean = new Gson().fromJson(response, LoginBean.class);
                if (bean.isSuccess()) {
                    EasySP.init(LoginActivity.this).putString("username", user).putString("psw",
                            psw);
                    EasySP.init(LoginActivity.this).putBoolean("isFirst", false);
                    Intent intent2 = new Intent(LoginActivity.this, MainActivity.class);
                    intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent2);
                } else {
                    ToastUtils.showShort(bean.getMessage());
                }
            }
        });
    }

    @NeedsPermission({Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void call() {
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        LoginActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode,
                grantResults);
    }

    @OnShowRationale({Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void call2(final PermissionRequest request) {
    }

    @OnClick({R.id.check_web_btn, R.id.login_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.check_web_btn:
                toSetBaseUrl();
                break;
            case R.id.login_btn:
//                Intent intent2 = new Intent(LoginActivity.this, MainActivity.class);
//                intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//                startActivity(intent2);
                login();
                break;
        }
    }

    private void toSetBaseUrl() {
        Intent intent = new Intent(this, BaseUrlManagerActivity.class);
//        intent.putExtra(BaseUrlManagerActivity.KEY_TITLE,"BaseUrl配置");
        startActivityForResult(intent, 101);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 101:
                    App.getBaseUrlManager().refreshData();
                    break;
            }
        }
    }
}
